package campshop.daos;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import campshop.models.UserOnline;
public class UserOnlineDao extends JdbcDaoSupport{
	public boolean addUserOnline(UserOnline uo)
	{
		String sql = "insert into " +
				"tbl_char_online (cs_uId, co_sIp, co_dLoginDate)" +
				" values (?, ?, ?)";
		if(super.getJdbcTemplate().update(sql, uo.getuId(), uo.getsIp(), uo.getdLoginDate())>0){
			return true;
		}else return false;
	}
	
	public boolean delUserOnline(long uId)
	{
		String sql = "delete from tbl_char_online where cs_uId = ?";
		if(super.getJdbcTemplate().update(sql, uId)>0) return true;
		return false;
	}
	
	public UserOnline getUserOnline(long uId)
	{
		String sql = "select * from tbl_char_online where cs_uId=" + uId;
		return (UserOnline)super.getJdbcTemplate().queryForObject(sql, new UserOnlineRowMapper());
	}
	
	class UserOnlineRowMapper implements RowMapper<UserOnline>{
		@Override
		public UserOnline mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserOnline uo = new UserOnline();
			uo.setuId(rs.getLong("cs_uId"));
			uo.setsIp(rs.getString("co_sIp"));
			uo.setdLoginDate(rs.getDate("co_dLoginDate"));
			return uo;
		}	
	}
}	
